package com.zxj.shop.admin.controller;




import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import com.zxj.shop.admin.shiro.RedisManager;
import com.zxj.shop.admin.shiro.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * https://gitee.com/whvse/EasyCaptcha?_from=gitee_search
 */

@Controller
@RequestMapping("/captcha")
@ResponseBody
public class CaptchaController {

	@Autowired
	RedisManager redisManager;

	@RequestMapping("/code")
	public ResultVO getCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 算术类型
		ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
		captcha.setLen(2);  // 几位数运算，默认是两位
		captcha.getArithmeticString();  // 获取运算的公式：3+2=?

		String verCode = captcha.text();  // 获取运算的结果：5
		String key = UUID.randomUUID().toString();
		// 存入redis并设置过期时间10分钟
		redisManager.set(key , verCode ,60 * 10);
		Map<String,String> cap = new HashMap<>();
		cap.put("key", key);
		cap.put("code",captcha.toBase64());
		return ResultVO.success(cap);
	}
}
